#ifndef KEYOPER_H_
#define KEYOPER_H_

#define KEYOPER_ERR_CURRGRP			-1  /*curren key group error */
#define KEYOPER_ERR_PARA			-2
#define KEY_GRP_SIZE( useage )  	(0x800 * (((useage) + (0x800 - 1)) / 0x800))

#define MT_GRP_ADDR             0
#define MT_GRP_SIZE             KEY_GRP_SIZE(MST_GRP_LEN * MT_GRP_MAX)		

#define FIX_GRP_ADDR            (MT_GRP_ADDR + MT_GRP_SIZE)
#define FIX_GRP_SIZE            KEY_GRP_SIZE(MST_GRP_LEN * FIX_GRP_MAX)

#define DUKPT_GRP_ADDR          (FIX_GRP_ADDR + FIX_GRP_SIZE)
#define DUKPT_GRP_SIZE          KEY_GRP_SIZE(DUKPT_GRP_LEN * DUKPT_GRP_MAX)

#define PEDPUB_CERT_ADDR        (DUKPT_GRP_ADDR + DUKPT_GRP_SIZE)
#define PEDPUB_CERT_SIZE        0x100

#define PUB_CERT_REMAINDER_ADDR  (PEDPUB_CERT_ADDR + PEDPUB_CERT_SIZE)
#define PUB_CERT_REMAINDER_SIZE  0x100

#define CRL_CERT_ADDR           (PUB_CERT_REMAINDER_ADDR + PUB_CERT_REMAINDER_SIZE)
#define CRL_CERT_SIZE           0x100

/* nvr_keymgt_t ->nvr_keymgt_t in NVSRAM */
#define KEY_STATUS_GET( fid, gid, type )	 	key_status_get( fid, gid, type )				
#define KEY_STATUS_SET( fid, gid, type )	 	key_status_set( fid, gid, type )
#define KEY_STATUS_UNSET( fid, gid, type )	key_status_unset( fid, gid, type )

#define KGP_STATUS_GET( fid, gid )	kgp_status_get( fid, gid )
#define KGP_STATUS_SET( fid, gid )	kgp_status_set( fid, gid )
#define KGP_STATUS_UNSET( fid, gid)	kgp_status_unset( fid, gid )

#define IS_MST_KEY( fid, gid, type )		((fid == KF_MT) && (type == KT_MST))

int magic_lock_test( void );
void get_dev_id(uint8_t * buf);
void read_recovery_pk(uint8_t * buf);
void read_vendor_pk(uint8_t * buf);
void security_start(void);

int mgt_set(uint32_t offset, uint8_t * buf, int buflen);
int mgt_get(uint32_t offset, uint8_t * buf, int buflen);
int mgt_set_bit( uint32_t offset, uint8_t value, uint8_t setflag );
void mgt_clear(void);

int key_field_init(void);
int key_field_write(uint32_t addr, uint8_t * buffer, uint32_t buflen);
int key_field_read(uint32_t addr, uint8_t * buffer, uint32_t buflen);
void key_field_clear(void);
void key_field_sha256(uint8_t * buf);
int save_pub_cert(uint8_t * buf, int len);
int get_pub_cert(uint8_t *buf, int len);

int des3_crypt(uint8_t *in, uint8_t *out, uint32_t len,
                uint8_t *key, int keynum, int decrypt);
int des_crypt(uint8_t *in, uint8_t *out, uint32_t len,
                uint8_t *key, int decrypt);
int des3_len( uint8_t * in, uint8_t * out, uint32_t inlen );
int des_len( uint8_t * in,  uint8_t * out, uint32_t inlen );

int save_future_key(uint8_t gid, uint8_t id, uint8_t * key);
int fetch_future_key(uint8_t gid, uint8_t id, uint8_t * key);
int get_future_key(uint8_t gid, uint8_t id, uint8_t * key);
int set_future_key(uint8_t gid, uint8_t id, uint8_t * key);

#endif /* KEYOPER_H_ */
